package com.pay.web.utils;

/**
 * Created by chenweiyi on 2018/1/30.
 */
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name){
        if(applicationContext == null){
            return null;
        }
        return (T) SpringContextUtils.applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        if(applicationContext == null){
            return null;
        }
        return SpringContextUtils.applicationContext.getBean(clazz);
    }


}

